package view;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;

import threads.AguardarServidor;

import controller.ControladorCliente;
import excecoes.ConexaoNaoIniciadaException;

import java.io.IOException;
import java.net.UnknownHostException;


public class DialogParticiparPartida extends JDialog {

	private static final long serialVersionUID = 1L;
	private FramePrincipal frame;
	private ControladorCliente controlador = null;  //  @jve:decl-index=0:
	private JPanel jContentPane = null;
	private JLabel servidor_jl = null;
	private JTextField ipServidor_tf = null;
	private JLabel apelido_jl = null;
	private JButton participar_b = null;
	private JLabel info_l = null;
	private JTextField apelido_tf = null;
	/**
	 * @param owner
	 */
	public DialogParticiparPartida(FramePrincipal owner) {
		super(owner);
		frame = owner;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(350, 273);
		this.setTitle("Participar de uma Partida");
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				fechar();
			}
		});
	}

	void fechar(){
		if (controlador != null) {
			controlador.fecharConexao();
		}
		this.setVisible(false);
		this.dispose();
		frame.setVisible(true);
		//System.exit(0);
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			info_l = new JLabel();
			info_l.setBounds(new Rectangle(76, 159, 211, 21));
			info_l.setText("");
			apelido_jl = new JLabel();
			apelido_jl.setBounds(new Rectangle(38, 70, 96, 21));
			apelido_jl.setText("Apelido:");
			servidor_jl = new JLabel();
			servidor_jl.setBounds(new Rectangle(38, 27, 97, 21));
			servidor_jl.setText("IP Servidor:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(servidor_jl, null);
			jContentPane.add(getIpServidor_tf(), null);
			jContentPane.add(apelido_jl, null);
			jContentPane.add(getApelido_tf(), null);
			jContentPane.add(getParticipar_b(), null);
			jContentPane.add(info_l, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes ipServidor_tf	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getIpServidor_tf() {
		if (ipServidor_tf == null) {
			ipServidor_tf = new JTextField();
			ipServidor_tf.setBounds(new Rectangle(173, 27, 129, 21));
		}
		return ipServidor_tf;
	}

	/**
	 * This method initializes apelido_tf	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getApelido_tf() {
		if (apelido_tf == null) {
			apelido_tf = new JTextField();
			apelido_tf.setBounds(new Rectangle(174, 70, 129, 21));
		}
		return apelido_tf;
	}

	/**
	 * This method initializes participar_b	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getParticipar_b() {
		if (participar_b == null) {
			participar_b = new JButton();
			participar_b.setBounds(new Rectangle(121, 120, 120, 24));
			participar_b.setText("Participar");
			participar_b.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					participarPartida();
				}
			});
		}
		return participar_b;
	}
	
	private void participarPartida(){
		String ipServidor = ipServidor_tf.getText();
		String apelido = apelido_tf.getText();
		
		if(!ipServidor.equals("") && !apelido.equals("")){
			try {
				controlador = ControladorCliente.getInstance();
				controlador.iniciarConexao(ipServidor);
				controlador.entrarNoJogo(apelido);
				
				AguardarServidor thread = new AguardarServidor(controlador,info_l,this);
				thread.start();
				
			} catch (UnknownHostException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
			} catch (ConexaoNaoIniciadaException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}


}  //  @jve:decl-index=0:visual-constraint="39,25"
